package com.company.woye.onpackage.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.company.woye.R;
import com.company.woye.onpackage.activity.OnPackageDetailActivity;
import com.company.woye.onpackage.entity.OnPackageDetailListEntity;
import com.company.woye.utils.Toaster;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OnPackageDetailListViewAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<OnPackageDetailListEntity> onPackageDetailListEntities;
    private State state;
    public static boolean allowChanged = false;

    public OnPackageDetailListViewAdapter(Context context, List<OnPackageDetailListEntity> onPackageDetailListEntities, State state) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.onPackageDetailListEntities = onPackageDetailListEntities;
        this.state = state;
    }

    @Override
    public int getCount() {
        return onPackageDetailListEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return onPackageDetailListEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHodler = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.intem_on_package_detail, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }
        viewHodler.tvOutState.setText(onPackageDetailListEntities.get(position).getOutState());

        viewHodler.tvOutState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("tvOutState.setOnClickListener-onClick");
                state.OutState(position);
            }
        });


        viewHodler.tvBoxNum.setText(onPackageDetailListEntities.get(position).getBoxNum());
        viewHodler.tvBw.setText(onPackageDetailListEntities.get(position).getBw());
        viewHodler.tvColor.setText(onPackageDetailListEntities.get(position).getColor());
        viewHodler.tvColorNo.setText(onPackageDetailListEntities.get(position).getColorNo());
        viewHodler.tvDBH.setText(onPackageDetailListEntities.get(position).getDBH());
        viewHodler.tvInScanPackNum.setText(onPackageDetailListEntities.get(position).getInScanPackNum());
        viewHodler.tvMaterielType.setText(onPackageDetailListEntities.get(position).getMaterielType());
        viewHodler.tvNum.setText(onPackageDetailListEntities.get(position).getNum());
        viewHodler.tvPackNum.setText(onPackageDetailListEntities.get(position).getPackNum());
        viewHodler.tvPo.setText(onPackageDetailListEntities.get(position).getPO());
        viewHodler.tvSerialNo.setText(onPackageDetailListEntities.get(position).getSerialNo());
        viewHodler.tvSize.setText(onPackageDetailListEntities.get(position).getSize());
        viewHodler.tvStyleNo.setText(onPackageDetailListEntities.get(position).getStyleNo());
        viewHodler.tvStyleSort.setText(onPackageDetailListEntities.get(position).getStyleSort());
        viewHodler.tvSewScanPackNum.setText(onPackageDetailListEntities.get(position).getSewScanPackNum());
//        if (allowChanged) {
//            viewHodler.cbOutState.setEnabled(false);
//        } else {
//            viewHodler.cbOutState.setEnabled(true);
//        }
        if (!allowChanged) {
            viewHodler.cbOutState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (allowChanged) {
//                    allowChanged=false;
                    String tmpDBH = onPackageDetailListEntities.get(position).getDBH();
                    if (b) {
                        if (!onPackageDetailListEntities.get(position).isAllowChanged()) {
                            for (int i = 0; i < onPackageDetailListEntities.size(); i++) {
                                System.out.println("tmpDBH:" + tmpDBH + " " + onPackageDetailListEntities.get(i).getDBH());
                                String DBH = onPackageDetailListEntities.get(i).getDBH();
                                if (DBH.equals(tmpDBH)) {
                                    onPackageDetailListEntities.get(i).setCheck(true);
                                    onPackageDetailListEntities.get(i).setAllowChanged(true);
                                } else if (DBH.indexOf("-") > -1) {
                                    System.out.println("FLDBH:" + DBH);
                                    System.out.println(DBH.substring(0, DBH.indexOf("-")));
                                    if (DBH.substring(0, DBH.indexOf("-")).equals(tmpDBH))
                                        onPackageDetailListEntities.get(i).setCheck(true);
                                    onPackageDetailListEntities.get(i).setAllowChanged(true);
                                } else if (tmpDBH.indexOf("-") > -1) {
                                    if (tmpDBH.substring(0, tmpDBH.indexOf("-")).equals(DBH))
                                        onPackageDetailListEntities.get(i).setCheck(true);
                                    onPackageDetailListEntities.get(i).setAllowChanged(true);
                                } else {
                                    if (!onPackageDetailListEntities.get(i).isCheck()) {
                                        onPackageDetailListEntities.get(i).setCheck(false);
                                    }
                                }
                            }
                        }
                    } else {
                        Log.e("1111", "falese" + position);
                        onPackageDetailListEntities.get(position).setCheck(false);
                        onPackageDetailListEntities.get(position).setAllowChanged(false);
                    }
                    notifyDataSetChanged();
//                    allowChanged=true;
//                }
                }
            });
        } else {
            viewHodler.cbOutState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        onPackageDetailListEntities.get(position).setCheck(true);
                    } else {
                        onPackageDetailListEntities.get(position).setCheck(false);
                    }
                }
            });
        }
        viewHodler.cbOutState.setChecked(onPackageDetailListEntities.get(position).isCheck());


        if (!TextUtils.isEmpty(onPackageDetailListEntities.get(position).getOutBoxNum())) {
            viewHodler.tv_minus.setTag(position);
            viewHodler.tv_add.setTag(position);
            viewHodler.tv_minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int OutBoxNum = Integer.parseInt(onPackageDetailListEntities.get(position).getOutBoxNum());
                    if (!TextUtils.isEmpty(OutBoxNum + "")) {
                        if (OutBoxNum > 1) {
                            OutBoxNum--;
                            onPackageDetailListEntities.get(position).setOutBoxNum(OutBoxNum + "");
                            notifyDataSetChanged();
                        } else {
                            Toast.makeText(mContext, "不能再减了", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
            viewHodler.tv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int OutBoxNum = Integer.parseInt(onPackageDetailListEntities.get(position).getOutBoxNum());
                    int BoxNum = Integer.parseInt(onPackageDetailListEntities.get(position).getBoxNum());
                    if (OutBoxNum < BoxNum) {
                        OutBoxNum++;
                        onPackageDetailListEntities.get(position).setOutBoxNum(OutBoxNum + "");
                        notifyDataSetChanged();
                    } else {
                        Toast.makeText(mContext, "出箱数不能大于箱数", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
        viewHodler.tv_OutBoxNum.setText(onPackageDetailListEntities.get(position).getOutBoxNum());
        return convertView;
    }

    static
    public class ViewHolder {
        @BindView(R.id.cb_OutState)
        CheckBox cbOutState;
        @BindView(R.id.tv_OutState)
        TextView tvOutState;
        @BindView(R.id.tv_DBH)
        TextView tvDBH;
        @BindView(R.id.tv_SerialNo)
        TextView tvSerialNo;
        @BindView(R.id.tv_StyleNo)
        TextView tvStyleNo;
        @BindView(R.id.tv_StyleSort)
        TextView tvStyleSort;
        @BindView(R.id.tv_bw)
        TextView tvBw;
        @BindView(R.id.tv_MaterielType)
        TextView tvMaterielType;
        @BindView(R.id.tv_po)
        TextView tvPo;
        @BindView(R.id.tv_Color)
        TextView tvColor;
        @BindView(R.id.tv_ColorNo)
        TextView tvColorNo;
        @BindView(R.id.tv_Size)
        TextView tvSize;
        @BindView(R.id.tv_Num)
        TextView tvNum;
        @BindView(R.id.tv_PackNum)
        TextView tvPackNum;
        @BindView(R.id.tv_InScanPackNum)
        TextView tvInScanPackNum;
        @BindView(R.id.tv_BoxNum)
        TextView tvBoxNum;
        @BindView(R.id.ll)
        LinearLayout ll;
        @BindView(R.id.tv_SewScanPackNum)
        TextView tvSewScanPackNum;
        @BindView(R.id.tv_minus)
        TextView tv_minus;
        @BindView(R.id.tv_add)
        TextView tv_add;
        @BindView(R.id.tv_OutBoxNum)
        TextView tv_OutBoxNum;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface State {
        void OutState(int position);
    }

}
